<template>
    <div class="title-wrapper">
        <el-skeleton animated :loading="loading">
            <template #template>
                <el-skeleton-item />
                <el-skeleton-item />
                <el-skeleton-item />
                <el-skeleton-item />
            </template>
            <template #default>
                <div class="title-box" v-if="detailData">
                    <div class="left-title">
                        <span class="title">{{ detailData.productName }}</span>
                        <el-tag :type="detailData.state === 'ENABLED' ? 'success' : 'info'">
                            {{ ProductState[detailData.state] }}
                        </el-tag>
                        <span class="desc">
                            本产品包含<span class="number"> {{ detailData.componentCount || 0 }} </span>
                            个组件；已配置<span class="number"> {{ detailData.deviceCount || 0 }} </span>个设备。
                        </span>
                    </div>
                    <div class="right-title">
                        <el-button
                            type="primary"
                            v-if="detailData.state == 'DISABLED'"
                            @click="onEnable(detailData)"
                            :loading="btnLoading"
                        >
                            启用
                        </el-button>
                        <el-button type="primary" v-else @click="onDisable(detailData)" :loading="btnLoading">
                            停用
                        </el-button>
                        <el-button @click="onGoPage">返回</el-button>
                    </div>
                </div>
            </template>
        </el-skeleton>
    </div>
</template>
<script setup lang="ts">
import { useRouter } from 'vue-router';
import { ProductState } from '@/utils/constant';
import { IoTProductService } from '@great/services';

const props = defineProps<{
    detailData?: Record<string, any>;
}>();
const router = useRouter();
const initDetail: any = inject('initDetail');

const loading = ref(true);
const btnLoading = ref(false);

const onGoPage = () => {
    router.push('/IoT/product/list');
};
// 停用
const onDisable = async row => {
    btnLoading.value = true;
    const res = await IoTProductService.verifyProductDeviceEnable(row?.id as number);
    btnLoading.value = false;
    if (res.data) {
        ElNotification({
            title: '该产品下设备正在使用中，不可停用！',
            type: 'error'
        });
    } else {
        ElMessageBox.confirm(`确认将产品及产品下任务全部停用？`, '提示', {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning'
        })
            .then(async () => {
                await IoTProductService.disableProduct(row?.id as number);
                ElNotification({
                    title: '停用成功！',
                    type: 'success'
                });
                initDetail();
            })
            .catch(() => {});
    }
};
// 启用
const onEnable = async row => {
    btnLoading.value = true;
    const res = await IoTProductService.verifyProductJobState(row?.id as number);
    btnLoading.value = false;
    ElMessageBox.confirm(`${res.data}`, '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
    })
        .then(async () => {
            await IoTProductService.enableProduct(row?.id as number);
            ElNotification({
                title: '启用成功！',
                type: 'success'
            });
            initDetail();
        })
        .catch(() => {});
};

watch(
    () => props.detailData,
    newValue => {
        if (newValue) loading.value = false;
    },
    { immediate: true }
);
</script>
<style scoped lang="scss">
.title-wrapper {
    .el-skeleton {
        display: flex;
        .el-skeleton__item {
            width: 20%;
            height: 32px;
        }
        .el-skeleton__item + .el-skeleton__item {
            margin-left: 10px;
            width: 60px;
        }
    }
    .title-box {
        display: flex;
        justify-content: space-between;
        height: 32px;
        align-items: center;
        .left-title {
            display: flex;
            align-items: center;
            .title {
                font-size: 18px;
                font-weight: 600;
            }
            .number {
                color: #409eff;
            }
            > span + span {
                margin-left: 10px;
            }
        }
    }
}
</style>
